<?php

namespace polymerize\user\module\user;


use app\common\InstanceTrait;
use app\model\User as UserModel;

class ChangeInfo
{
    use InstanceTrait;

    /**
     * 修改用户信息
     *
     * @param int    $uid
     * @param string $type
     * @param string $avatar
     * @param string $nickname
     * @param int    $sex
     * @param string $birthday
     * @return bool
     */
    public function changeInfo(int $uid, string $type, string $avatar, string $nickname, int $sex, string $birthday): bool
    {
        $where[] = ['id', '=', $uid];

        switch ($type) {
            case 'avatar':
                //删除 http://ps-owenweb.oss-cn-shanghai.aliyuncs.com/
                if (strstr($avatar, '.aliyuncs.com/')) {
                    $avatar = getLaterString($avatar, '.aliyuncs.com/');
                }
                $data = ['avatar' => $avatar];
                break;
            case 'nickname':
                $data = ['nickname' => $nickname];
                break;
            case 'sex':
                $data = ['sex' => $sex];
                break;
            case 'birthday':
                $data = ['birthday' => $birthday];
                break;
            default:
                return false;
        }

        $data['u_time'] = myDate();
        return UserModel::updateDataCache($where, $data);
    }
}
